﻿<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Profile" %>
<%@ Import Namespace="System.Collections.Generic" %>
<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        //在应用程序启动时运行的代码
        #region 网站数据初始化
        try
        {
            //调试BUG用，如果正式发布，注释掉此句话
#if DEBUG
            GoShopping.BLL.Databases.DebugSql();
#endif

            MembershipCreateStatus statue;
            GoShopping.BLL.MemberShipService mem = new GoShopping.BLL.MemberShipService();
            if (mem.IsMembershipDBHasData() == false)
            {
                GoShopping.BLL.SiteSetting site = new GoShopping.BLL.SiteSetting();
                System.Net.Configuration.SmtpSection section = site.GetSmtpSection();
                bool b = mem.CreateSuperManager(section.From, section.Network.Password, out statue);
                if (b == false)
                {
                    throw (new Exception("CreateSuperManager:" + statue.ToString()));
                }
            }
        }
        catch
        {
            throw (new Exception("数据库无法连接！"));
        }
        #endregion
    }
    void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs e)
    {
        // get the profile for the anonymous user        
        ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);
        // 购物车
        // if they have a shopping cart, then migrate that to the authenticated user
        if (anonProfile.grpShoppingCart.MyCart != null)
        {
            if (Profile.grpShoppingCart.MyCart.Items == null)
                Profile.grpShoppingCart.MyCart.Items = new List<CartItem>();

            IEnumerator ie = anonProfile.grpShoppingCart.MyCart.Items.GetEnumerator();
            while (ie.MoveNext())
            {
                Profile.grpShoppingCart.MyCart.AddToCart((CartItem)(ie.Current));
            }
            //Profile.grpShoppingCart.MyCart.Items.AddRange(anonProfile.grpShoppingCart.MyCart.Items);
            anonProfile.grpShoppingCart.MyCart = null;
        }
        //最近浏览的商品
        if (anonProfile.grpRecent != null)
        {
            if (Profile.grpRecent == null)
                Profile.grpRecent = new RecentPrdts();
            IEnumerator ie1 = anonProfile.grpRecent.RecentGoods.GetEnumerator();
            while (ie1.MoveNext())
            {
                Profile.grpRecent.AddRecent((RecentPrdt)(ie1.Current));
            }
            anonProfile.grpRecent.RecentGoods = null;
            //Profile.Favourite.LatestGoodsId.AddRange(anonProfile.Favourite.LatestGoodsId);
        }

        ProfileManager.DeleteProfile(e.AnonymousID);
        AnonymousIdentificationModule.ClearAnonymousIdentifier();
        Profile.Save();
    }
    void Application_End(object sender, EventArgs e) 
    {
        //在应用程序关闭时运行的代码

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        //在出现未处理的错误时运行的代码
        Exception erroy = Server.GetLastError();
        string err = string.Empty;
        try
        {
            err += "出错页面是：" + Request.Url.ToString() + "< br>";
        }
        catch { }
        err += "异常信息：" + erroy.Message + "< br>";
        err += "Source:" + erroy.Source + "< br>";
        err += "StackTrace:" + erroy.StackTrace + "< br>";
        Server.ClearError();
        Application["error"] = err;
    }

    void Session_Start(object sender, EventArgs e) 
    {
        //在新会话启动时运行的代码

    }

    void Session_End(object sender, EventArgs e) 
    {
        //在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时，才会引发 Session_End 事件。如果会话模式 
        //设置为 StateServer 或 SQLServer，则不会引发该事件。

    }
</script>
